package demo7;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class InputFormatJob {

    public static void main(String[] args) throws Exception {
        // 1. 初始化配置
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop10:9000");
        //2. 创建job
        Job job = Job.getInstance(conf);
        job.setJarByClass(InputFormatJob.class);

        //3. 设置输入格式化工具和输出格式化
        job.setOutputFormatClass(TextOutputFormat.class);

        //4. 设置输入路径和输出路径

        // TextInputFormat:默认设置，读取文件夹，如果包含多个小文件会对应多个split,同时会启动多个maptask
//        job.setInputFormatClass(TextInputFormat.class);
//        TextInputFormat.addInputPath(job, new Path("mapreduce/demo7/"));

        // CombineTextInputFormat(优化）：优化小文件读取，防止启动过多的MapTask
        job.setInputFormatClass(CombineTextInputFormat.class);
        // 只要读取的block块数据不超过10M，就属于一个split逻辑切片，对应只会启动要给的mapTask
        CombineTextInputFormat.setMaxInputSplitSize(job,10485760); // 10485760字节 = 10M
        CombineTextInputFormat.addInputPath(job,new Path("/mapreduce/demo7/"));
        TextOutputFormat.setOutputPath(job, new Path("mapreduce/demo7/out"));
        //5. 设置mapper和reducer
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        // 6. 设置mapper的kv类型和reducer的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 7. 启动job
        boolean b = job.waitForCompletion(true);
        System.out.println(b);
    }

    static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        /**
         * 执行时机：每读取一行k-v，调用一次map方法
         *
         * @param key     输入k
         * @param value   输入v
         * @param context 输出k-v写出工具。
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //1. 接受k-v
            //2. 对v进行拆分
            String sv = value.toString();
            String[] names = sv.split(" ");
            //遍历数组，将得到每个name，作为k输出。
            for (String name : names) {
                //3. 将k(name)-v(1)
                context.write(new Text(name), new IntWritable(1));
            }

        }
    }

    static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        /**
         * 执行时机：每读取Reduce端合并后的一组数据(k-vs),调用一次reduce方法。
         *
         * @param key     输入k
         * @param values  输入value [1,2,3,1]
         * @param context 输出k-v
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            //1: 接受k-vs
            //2. 对vs 遍历累加
            int sum = 0;
            for (IntWritable value : values) {
                sum = sum + value.get();
            }
            //3. 输出
            // k(name)-v(累加和)
            context.write(key, new IntWritable(sum));
        }
    }

}
